package com.xunsiya.controller.order;

import com.alibaba.fastjson.JSONObject;
import com.xunsiya.common.config.RequestUtils;
import com.xunsiya.module.order.vo.*;
import com.xunsiya.service.module.order.OrderService;
import com.xunsiya.service.system.CompanyService;
import com.xunsiya.system.vo.Company;
import com.xunsiya.system.vo.User;
import com.xunsiya.tools.common.jsonobject.RequestJson;
import com.xunsiya.tools.common.msg.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 订单管理
 *
 * @author panjun
 * @version 1.0
 *          <p>
 *          <p>修订人		修订时间			描述信息
 *          <p>-----------------------------------------------------
 *          <p>panjun		2018/10/31		初始创建
 */
@RestController
@RequestMapping("/api/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private CompanyService companyService;


    /**
     * 查询用户订单
     *
     * @return
     */
    @PostMapping("/myOrders")
    public Msg myOrders() {

        Integer pageSize = RequestUtils.getIntParam("pageSize");

        // 当前用户
        User user = RequestUtils.getSessionAttribte(User.SYS_LOGIN_INFO);
        //根据公司编号查询公司信息
        Company company = companyService.findParentId(user.getCompanyId());
        List<Integer> companyIds = new ArrayList<>();
        companyIds.add(user.getCompanyId());

        Integer parentId = company.getParentId();

        // 如果不是母公司
        while (parentId != 0) {
            company = companyService.findParentId(parentId);
            companyIds.add(parentId);
            parentId = company.getParentId();
        }

        List<Long> ids = new ArrayList<>();

        // 查询用户订单
        List<Order> sysOrderList = orderService.findByCompanyId(companyIds, 0, pageSize);

        Map<Long, Order> map = new HashMap<>(8);
        sysOrderList.forEach(sysOrder -> {
            ids.add(sysOrder.getId());
            map.put(sysOrder.getId(), sysOrder);
        });

        // 没有订单
        if (CollectionUtils.isEmpty(ids)) {
            return new Msg(true);
        }

        // 查询订单详情
        List<OrderDetail> sysOrderDetailList = orderService.findOrderDetailsByOrderIds(ids);
        List<ProductSrvVo> productSrvList = new ArrayList<>();
        // 当前时间
        Date nowDate = new Date();
        sysOrderDetailList.forEach(sysOrderDetail -> {
            if (sysOrderDetail.getOutDate().compareTo(nowDate) > 0) {
                ProductSrvVo productSrv = new ProductSrvVo();
                productSrv.setOrderId(sysOrderDetail.getOrderId() + "");
                productSrv.setOrder(map.get(sysOrderDetail.getOrderId()));
                productSrv.setOrderDetail(sysOrderDetail);
                productSrvList.add(productSrv);
            }
        });

        return new Msg(true, "", productSrvList);
    }

    /**
     * 添加订单
     *
     * @param orderDto 订单信息
     * @return
     */
    @PostMapping("/submit")
    public Msg submit(@RequestBody OrderDto orderDto) {

        // 保存订单
        boolean bool = orderService.save(orderDto);
        return new Msg(bool, "保存成功");
    }

    /**
     * 分页查询企业订单列表
     *
     * @param requestBody
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @RequestMapping("/queryForPage")
    public Msg queryForPage(@RequestBody JSONObject requestBody,
                            @RequestParam(value = "pageIndex", required = false, defaultValue = "1") Integer pageIndex,
                            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        Company company = new Company();
        company.setId(RequestJson.getIntegerNoThrow(requestBody, "companyId"));
        return orderService.queryForPage(company, pageIndex, pageSize);
    }

}
